/*
    Android UIML

	Copyright (C) 2010  Bram Goffings (bramgoffings@gmail.com)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public License
	as published by the Free Software Foundation; either version 2.1
	of	the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

package uiml.android.executing;

import org.w3c.dom.Element;

import uiml.android.elements.Logic;
import uiml.android.elements.Part;
import uiml.android.elements.UimlElementWithAttributes;
import uiml.android.elements.peers.Vocabulary;

public class Rule extends UimlElementWithAttributes{
	
	// TODO comment
	private Condition condition = null;
	private Action action = null;
	
	// TODO comment
	public static String IAM = "rule";
	public static String CONDITION = "condition";
	public static String ACTION = "action";
	
	/**
	 * 
	 * @param node
	 */
	public Rule(Element node){
		super(node);
		process();
		
		// attach action to condition
		if(condition != null && action != null)
			condition.attach(action);
	}

	/**
	 * 
	 */
	@Override
	public void processElement(Element elem) {
		if(elem.getTagName().equals(CONDITION))
			setCondition(new Condition(elem));
		else if(elem.getTagName().equals(ACTION))
			setAction(new Action(elem));
	}

	/**
	 * @param condition the condition to set
	 */
	public void setCondition(Condition condition) {
		this.condition = condition;
	}

	/**
	 * @return the condition
	 */
	public Condition getCondition() {
		return condition;
	}

	/**
	 * @param action the action to set
	 */
	public void setAction(Action action) {
		this.action = action;
	}

	/**
	 * @return the action
	 */
	public Action getAction() {
		return action;
	}
	
	/**
	 * Shortcut naar de part-name van deze rule
	 */
	public String getPartNameInvoker() {
		if(condition == null)
			return null;
		else
			return condition.getPartNameInvoker();
	}

	/**
	 * 
	 * @param obj
	 * @param uiPart
	 * @param logic 
	 */
	public void execute(Object obj, Part uiPart, Vocabulary voc, Logic logic) {
		if(condition != null) {
			condition.execute(obj, uiPart, voc, logic);		
		}
	}
	
	

}
